package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

public class LoginDemo {
    public static void main(String[] args) {
        try(
                Connection connection = DBUtil.getConnection();
                Scanner scanner = new Scanner(System.in)
        ){
            Statement statement = connection.createStatement();
//            System.out.println("请输入用户名");
//            String username = scanner.nextLine();
//            System.out.println("请输入密码");
//            String password = scanner.nextLine();
//            String sql = "SELECT username, password " +
//                    "FROM userinfo " +
//                    "WHERE username = '"+ username + "' AND password = '"
//                    + password + "'";
//            ResultSet rs = statement.executeQuery(sql);
//            while(rs.next()){
//                String username1 = rs.getString("username");
//                String password1 = rs.getString("password");
//                if(username.equals(username1) && password.equals(password1)){
//                    System.out.println("登录成功");
//                    return;
//                }
//            }
//            System.out.println("登录失败");
            UserInfo userInfo = InputUtil.getInputObject(new UserInfo(),"欢迎登录","登录");
            String sql = "SELECT id,username, password,nickname,age " +
                    "FROM userinfo " +
                    "WHERE username = '"+ userInfo.getUsername() + "' AND password = '"
                    + userInfo.getPassword() + "'";
            ResultSet rs = statement.executeQuery(sql);
            if (rs.next()){
                System.out.println("登录成功，欢迎您:"+rs.getString("nickname"));
            }else {
                System.out.println("登录失败,用户名或密码不正确");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
